package com.sitech.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.sitech.ibnms.util.IDUtil;
import org.apache.struts2.ServletActionContext;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;

import com.opensymphony.xwork2.ActionContext;
import com.sitech.BaseConst;
import com.sitech.portal.system.dao.ISysLogDao;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;

public class Logging {
	private ISysLogDao sysLogDao;
	
	public ISysLogDao getSysLogDao() {
		return sysLogDao;
	}

	public void setSysLogDao(ISysLogDao sysLogDao) {
		this.sysLogDao = sysLogDao;
	}
	public void after(JoinPoint joinpoint) {
		//joinpoint.getArgs();// 此方法返回的是一个数组，数组中包括request以及ActionCofig等类对象
		
		
		
		System.out.println("被拦截方法调用之前调用此方法，输出此语句");
	}

	public void before(JoinPoint joinpoint) {
		
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);  
		TblSYS_LOGINMSG user = (TblSYS_LOGINMSG)request.getSession().getAttribute(BaseConst.SESSION_USER_KEY);
		
		Signature signature = joinpoint.getSignature();
		String className = signature.getDeclaringTypeName();
		String methodName = signature.getName();
		
		Object[] obj = joinpoint.getArgs();
		JSONArray jsonArray = JSONArray.fromObject(obj); 
		Map map = new HashMap();
		map.put("LOGIN_ID", user.getLOGIN_ID());
		map.put("OPERATE_TIME", new java.util.Date());
		map.put("OPERATE_CLASS", className);
		map.put("OPERATE_METHOD", methodName);
		map.put("OPERATE_ARGS", jsonArray.toString());
		map.put("ID", IDUtil.randomEncodedUUID());
		String rawdata = "";
		//Map jsonobjMap = (Map)JSONObject.fromObject(obj[0]);
		if(obj!=null&&obj.length>0){
			String tabName="";
			String tabId="";
			String paramId="";
			Map cfgMap =  sysLogDao.getTB_SYS_LOGGING_CONFIGByAOP(className, methodName);
			if(cfgMap!=null){
				tabName = cfgMap.get("DATA_TAB")==null?"":cfgMap.get("DATA_TAB").toString();
				 tabId = cfgMap.get("TAB_ID")==null?"":cfgMap.get("TAB_ID").toString();
				 paramId = cfgMap.get("PARAM_ID")==null?"":cfgMap.get("PARAM_ID").toString();
			}			 
			Object[] ids =null;
			if(methodName.startsWith("del")&&cfgMap!=null){				
				if(obj[0].getClass().getCanonicalName().endsWith("[]")){
					ids = (Object[])obj[0];
				}else{
					ids = new Object[1];
					ids[0] = obj[0];
				}
			}
			if(methodName.startsWith("update")&&cfgMap!=null){	
				ids = new Object[1];
				for(int i=0;i<obj.length;i++){
					if(!obj[i].getClass().getCanonicalName().endsWith("[]")){
						Map jsonobjMap = (Map)JSONObject.fromObject(obj[i]);
						if(jsonobjMap.get(paramId)!=null){
							ids[0] = jsonobjMap.get(paramId);
							break;
						}
					}
				}
				
			}
			
			if(tabName!=""&&tabId!=""){
				Map paramMap = new HashMap();
				paramMap.put("tabName", tabName);
				paramMap.put("tabId", tabId);
				paramMap.put("ids", ids);
				List rasList = sysLogDao.getTbinfoByIds(paramMap);
				if(rasList!=null&&rasList.size()>0){
					JSONArray rawArray = JSONArray.fromObject(rasList); 
					rawdata = rawArray.toString();
				}
			}
			
		}
		
		map.put("RAW_DATA", rawdata);	
		sysLogDao.loggingAdd(map);
		
		System.out.println("被拦截方法调用之后调用此方法，输出此语句:调用类-->"+className+",调用方法-->"+methodName+",操作人-->"+user.getNAME());
		System.out.println("操作参数："+jsonArray.toString());
	}
	
	public static void main(String[] args){
		Object[] obj = new String[]{"abc"};
		JSONArray jsonArray = JSONArray.fromObject(obj); 
		System.out.println(jsonArray.toString());
	}
}
